package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.mms.pojo.MmsDeptExtraInfoEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.pojo.GpmProjectSuperviseEntity;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.gpm.service.impl.GpmProjectSuperviseService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

public class GpmProjectSuperviseScript extends GpmProjectScript {
    /**
     * @author: zhangjunyao
     * @date: 2021-12-20
     * @description: 督办审批开始后提示财总。
     * @return:
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void remindCFO(ITapService service, TapEntity entity) {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        // sourcetype = 'MpmProjectSupervise'
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        GpmProjectSuperviseService gpmProjectSuperviseService = (GpmProjectSuperviseService) SpringUtil.getBean("gpmProjectSuperviseService");
        ICboMessageAppService cboMessageAppService = (ICboMessageAppService) SpringUtil.getBean("cboMessageAppService");
        ICboMessageSysService cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        //查询项目entity
        GpmProjectSuperviseEntity gpmProjectSuperviseEntity = gpmProjectSuperviseService.queryById(sourceid);
        // 如果是系统发送的督办，就不用给CFO发送消息
        if (gpmProjectSuperviseEntity.getSendresource() == 1) {
            return;
        }
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("id", gpmProjectSuperviseEntity.getId());
        String selectName = "getCFOCode";
        List<Map<String, Object>> res = gpmProjectSuperviseService.queryMap(paraMap, selectName);
        // 插入App消息
        if (CollectionUtils.isEmpty(res)) {
            return;
        }
        String usercode = res.get(0).get(MmsDeptExtraInfoEntity.FieldCfocode).toString();
        String username = res.get(0).get(MmsDeptExtraInfoEntity.FieldCfoname).toString();
        String title = "SD项目督办单提醒";
        String content = "【" + gpmProjectSuperviseEntity.getProjectname() + "】督办单:" +
                gpmProjectSuperviseEntity.getSupervisename() + "，已下发，责任人" +
                gpmProjectSuperviseEntity.getResponsiblename() + "，请知悉";
        Map<String, Object> projectappmap = insertMessageApp(usercode, title, content,
                ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        cboMessageAppService.insertMap(projectappmap);
        // 系统消息
        Map<String, Object> sysMap = insertMessageSys(usercode, username, title, content,
                ConvertUtil.convertToLong(sourceid), sourcetype);
        cboMessageSysService.insertMap(sysMap);
    }

    /**
     * @author: zhangjunyao
     * @date: 2021-12-20
     * @description: 督办PMO审批通过后提示项目经理、省总和财总。
     * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void remindManagerCEOCFO(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
        GpmProjectSuperviseService gpmProjectSuperviseService = (GpmProjectSuperviseService) SpringUtil.getBean("gpmProjectSuperviseService");
        GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
        ICboMessageAppService cboMessageAppService = (ICboMessageAppService) SpringUtil.getBean("cboMessageAppService");
        ICboMessageSysService cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        // 查询项目督办entity
        GpmProjectSuperviseEntity gpmProjectSuperviseEntity = gpmProjectSuperviseService.queryById(sourceid);
        // 查询项目entity
        StringBufferProxy str = new StringBufferProxy();
        str.appendSingle("projectcode = '{0}'", gpmProjectSuperviseEntity.getProjectcode());
        List<GpmProjectEntity> sdProjectEntities = gpmProjectService.queryByWhere(str.toString());
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("id", gpmProjectSuperviseEntity.getId());
        // 查询CEO和CFO信息
        List<Map<String, Object>> cfoInfo = gpmProjectSuperviseService.queryMap(paraMap, "getCFOCode");
        List<Map<String, Object>> ceoInfo = gpmProjectSuperviseService.queryMap(paraMap, "getCEOCode");
        if (CollectionUtils.isEmpty(cfoInfo) || CollectionUtils.isEmpty(ceoInfo) || CollectionUtils.isEmpty(sdProjectEntities)) {
            return;
        }
        // 消息内容
        String title = "SD项目督办单提醒";
        String content = "【" + gpmProjectSuperviseEntity.getProjectname() + "】督办单:" +
                gpmProjectSuperviseEntity.getSupervisename() + "，已下发，责任人" +
                gpmProjectSuperviseEntity.getResponsiblename() + "，请知悉";
        // 接收消息人信息
        String cfoUserCode = cfoInfo.get(0).get(MmsDeptExtraInfoEntity.FieldCfocode).toString();
        String cfoUserName = cfoInfo.get(0).get(MmsDeptExtraInfoEntity.FieldCfoname).toString();
        String ceoUserCode = ceoInfo.get(0).get(MmsDeptExtraInfoEntity.FieldProvincemanagercode).toString();
        String ceoUserName = ceoInfo.get(0).get(MmsDeptExtraInfoEntity.FieldProvincemanagername).toString();
        String projectManagerCode = sdProjectEntities.get(0).getProjectmanagercode();
        String projectManager = sdProjectEntities.get(0).getProjectmanager();
        // 若CEO和项目经理或CFO和项目经理为同一人则不重发消息
        boolean managerEqualsCEOCFO = projectManagerCode.equals(cfoUserCode) || projectManagerCode.equals(ceoUserCode);
        // 插入App消息
        Map<String, Object> cfoAppMap = insertMessageApp(cfoUserCode, title, content, ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        Map<String, Object> ceoAppMap = insertMessageApp(ceoUserCode, title, content, ConvertUtil.convertToLong(sourceid), sourcetype, 0);
        if (!managerEqualsCEOCFO) {
            Map<String, Object> projectMangerAppMap = insertMessageApp(projectManagerCode, title, content, ConvertUtil.convertToLong(sourceid), sourcetype, 0);
            cboMessageAppService.insertMap(projectMangerAppMap);
        }
        cboMessageAppService.insertMap(cfoAppMap);
        cboMessageAppService.insertMap(ceoAppMap);
        // 系统消息
        Map<String, Object> cfoSysMap = insertMessageSys(cfoUserCode, cfoUserName, title, content, ConvertUtil.convertToLong(sourceid), sourcetype);
        Map<String, Object> ceoSysMap = insertMessageSys(ceoUserCode, ceoUserName, title, content, ConvertUtil.convertToLong(sourceid), sourcetype);
        if (!managerEqualsCEOCFO) {
            Map<String, Object> projectManagerSysMap = insertMessageSys(projectManagerCode, projectManager, title, content, ConvertUtil.convertToLong(sourceid), sourcetype);
            cboMessageSysService.insertMap(projectManagerSysMap);
        }
        cboMessageSysService.insertMap(cfoSysMap);
        cboMessageSysService.insertMap(ceoSysMap);
    }

    /**
     * @author: zhangjunyao
     * @date: 2021-12-20
     * @description: 若填写了抄送人员范围则给抄送人发送App提醒。
     * @return: com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void remindCcStaff (ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        GpmProjectSuperviseService gpmProjectSuperviseService = (GpmProjectSuperviseService) SpringUtil.getBean("gpmProjectSuperviseService");
        ICboMessageAppService cboMessageAppService = (ICboMessageAppService) SpringUtil.getBean("cboMessageAppService");
        ICboMessageSysService cboMessageSysService = (ICboMessageSysService) SpringUtil.getBean("cboMessageSysService");
        // 查询项目督办entity
        GpmProjectSuperviseEntity gpmProjectSuperviseEntity = gpmProjectSuperviseService.queryById(sourceid);
        String ccStaffs = gpmProjectSuperviseEntity.getCcstaffcode();
        // 如果手动下发时没有选择抄送人，就不发消息；自动下发督办在开始节点无法指定抄送人。如果在PMO审核节点指定抄送人，会发送消息。
        if (ccStaffs == null) {
            return;
        }
        // 督办单信息
        String title = "SD项目督办单提醒";
        String content = "【" + gpmProjectSuperviseEntity.getProjectname() + "】督办单:" + gpmProjectSuperviseEntity.getSupervisename()
                + "，已下发，责任人" + gpmProjectSuperviseEntity.getResponsiblename() + "，请知悉";
        // app信息处理
        List<Map<String, Object>> appMessageList = new ArrayList<>();
        List<Map<String, Object>> sysMessageList = new ArrayList<>();
        String[] ccStaffCodeList = ccStaffs.split(",");
        List<String> ccStaffNameList = Arrays.asList(gpmProjectSuperviseEntity.getCcstaffname().split("，"));
        int count = 0;
        for (String code : ccStaffCodeList) {
            String ccStaffName = ccStaffNameList.get(count);
            Map<String, Object> appMessageMap = insertMessageApp(code, title, content,
                    ConvertUtil.convertToLong(gpmProjectSuperviseEntity.getId()), GpmProjectSuperviseEntity.tableName, 0);
            appMessageList.add(appMessageMap);
            Map<String, Object> sysMessageMap = insertMessageSys(code, ccStaffName, title, content,
                    ConvertUtil.convertToLong(gpmProjectSuperviseEntity.getId()), GpmProjectSuperviseEntity.tableName);
            sysMessageList.add(sysMessageMap);
            count ++;
        }
        // 发送信息
        cboMessageAppService.insertList(appMessageList);
        cboMessageSysService.insertList(sysMessageList);
    }

    /**
     * @author: zhangjunyao
     * @date: 2021-12-20
     * @description: 督办审批通过后更新督办单状态
     * @return: void
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateProjectSupervise(ITapService service, TapEntity entity) throws Exception {
        //审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        GpmProjectSuperviseService gpmProjectSuperviseService = (GpmProjectSuperviseService) SpringUtil.getBean("gpmProjectSuperviseService");
        GpmProjectSuperviseEntity gpmProjectSuperviseEntity = gpmProjectSuperviseService.queryById(sourceid);
        gpmProjectSuperviseEntity.setSupervisestate("2");
        gpmProjectSuperviseService.updateEntity(gpmProjectSuperviseEntity);
    }

    /**
     * @author: zhangjunyao
     * @date: 2021-12-20
     * @description: 插入系统消息
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @modify:
     */
    public Map<String, Object> insertMessageSys(String usercode, String username, String title, String content, long sourceid, String sourcetype) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("receivercode", usercode);
        map.put("receivername", username);
        map.put("subject", title);
        map.put("content", content);
        map.put("sourceid", sourceid);
        map.put("sourcetype", sourcetype);
        return map;
    }

    /**
     * @author: zhangjunyao
     * @date: 2021-12-20
     * @description: 插入App消息
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @modify:
     */
    public Map<String, Object> insertMessageApp(String usercode, String extendfield1, String content, long sourceid, String sourcetype, int sendtype) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("usercode", usercode);
        map.put("extendfield1", extendfield1);
        map.put("content", content);
        map.put("sourceid", sourceid);
        map.put("sourcetype", sourcetype);
        map.put("sendtype", sendtype);
        return map;
    }
}
